/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkl51.validasi;

import java.util.List;
import pkl51.component.TextField;
import pkl51.controller.CardKues;
import pkl51.view.kuesioner.PanelBlok4;
import pkl51.view.kuesioner.PanelBlok123;

/**
 *
 * @author ERI
 */
public class ValidasiBlok4 extends AbstractValidasi {

    private PanelBlok4 panelBlok4;
    public String dataRuta[][]=new String[15][2];

    public void setPanelBlok4(PanelBlok4 panelBlok4) {
        this.panelBlok4 = panelBlok4;
    }

    public String[][] getDataRuta() {
        return dataRuta;
    }

    public void registerValidator() {
        for (int i = 0; i < 15; i++) {
            List<TextField> list = panelBlok4.getListTextFieldBlok4().subList((i * 11), 11 + (i * 11));
            for (int j = 0; j < 11; j++) {
                switch (j) {
                    case 0:
                        list.get(j).setValidator(new ValidasiB4C2());
                        break;
                    case 1:
                        list.get(j).setValidator(new ValidasiB4C3());
                        break;
                    case 2:
                        list.get(j).setValidator(new ValidasiB4C4());
                        break;
                    case 3:
                        list.get(j).setValidator(new ValidasiB4C5());
                        break;
                    case 4:
                        list.get(j).setValidator(new ValidasiB4C6());
                        break;
                    case 5:
                        list.get(j).setValidator(new ValidasiB4C7());
                        break;
                    case 6:
                        list.get(j).setValidator(new ValidasiB4C8());
                        break;
                    case 7:
                        list.get(j).setValidator(new ValidasiB4C9());
                        break;
                    case 8:
                        list.get(j).setValidator(new ValidasiB4C10());
                        break;
                    case 9:
                        list.get(j).setValidator(new ValidasiB4C11());
                        break;
                    case 10:
                        list.get(j).setValidator(new ValidasiB4C12());
                        break;

                }
            }
        }

    }

    public void unRegisterValidator() {
        for (int i = 0; i < 15; i++) {
            List<TextField> list = panelBlok4.getListTextFieldBlok4().subList((i * 11), 11 + (i * 11));
            for (int j = 0; j < 11; j++) {
                switch (j) {
                    case 0:
                        list.get(j).setValidator(new NullValidasi());
                        break;
                    case 1:
                        list.get(j).setValidator(new NullValidasi());
                        break;
                    case 2:
                        list.get(j).setValidator(new NullValidasi());
                        break;
                    case 3:
                        list.get(j).setValidator(new NullValidasi());
                        break;
                    case 4:
                        list.get(j).setValidator(new NullValidasi());
                        break;
                    case 5:
                        list.get(j).setValidator(new NullValidasi());
                        break;
                    case 6:
                        list.get(j).setValidator(new NullValidasi());
                        break;
                    case 7:
                        list.get(j).setValidator(new NullValidasi());
                        break;
                    case 8:
                        list.get(j).setValidator(new NullValidasi());
                        break;
                    case 9:
                        list.get(j).setValidator(new NullValidasi());
                        break;
                    case 10:
                        list.get(j).setValidator(new NullValidasi());
                        break;

                }
            }
        }
    }

    private class NullValidasi implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            return true;
        }
    }

    private class ValidasiB4C2 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = panelBlok4.getListTextFieldBlok4().indexOf(field);
            if (index == 0) {
                if (!field.getText().equals(PanelBlok123.getInstance().getTextFieldB1R8().getText())) {
                    errorMessage("Nama KRT tidak sesuai!");
//                    System.out.println(field.getText());
//                    System.out.println(PanelBlok123.getInstance().getTextFieldB1R8().getText());
                    return false;
                } else {
                    return true;
                }
            } else {
                return true;
            }
        }
    }

    private class ValidasiB4C3 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = panelBlok4.getListTextFieldBlok4().indexOf(field);
            boolean allFilled = true;
            for (int i = 0; i < index; i++) {
                if (panelBlok4.getListTextFieldBlok4().get(i).getText().equals("") && panelBlok4.getListTextFieldBlok4().get(i).isEnabled()) {
                    allFilled = false;
                }
            }
            int rowNumber = (index + 9) % 10;

            if (allFilled == true) {
                if (Integer.parseInt(field.getText()) >= 1 && Integer.parseInt(field.getText()) <= 9) {

                    if (index == 1) {
                        if (field.getText().equals("1")) {
                            return true;
                        } else {
                            errorMessage("Hubungan dengan KRT harus 1 pada baris pertama!");
                            return false;
                        }
                    } else {
                        if (field.getText().equals("2")) {
                            boolean urutanBenar = true;
                            for (int i = 0; i < rowNumber; i++) {
                                int listIndex = 1 + i + (i * 10);
                                System.out.println("");
                                if (Integer.parseInt(panelBlok4.getListTextFieldBlok4().get(listIndex).getText()) > 2) {
                                    urutanBenar = false;
                                    break;
                                }
                            }

                            if (urutanBenar == false) {
                                errorMessage("Urutan Suami/Istri harus setelah KRT (atau Suami/Istri lainnya)!");
                                return false;
                            } else if (panelBlok4.getListTextFieldBlok4().get(4).getText().equals("2")) {
                                return true;
                            } else {
                                errorMessage("Hubungan dengan KRT tidak mungkin 2!");
                                return false;
                            }
                        } else {
                            if (field.getText().equals("1")) {
                                errorMessage("Hubungan dengan KRT tidak boleh 1 selain baris pertama!");
                                return false;
                            } else {
                                return true;
                            }
                        }
                    }
                } else {
                    errorMessage("Nilai field antara 1 atau 9!");
                    return false;
                }
            } else {
                errorMessage("Isi Field sebelumnya terlebih dahulu!");
                return false;
            }

        }
    }

    private class ValidasiB4C4 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = panelBlok4.getListTextFieldBlok4().indexOf(field);
            boolean allFilled = true;
            for (int i = 0; i < index; i++) {
                if (panelBlok4.getListTextFieldBlok4().get(i).getText().equals("") && panelBlok4.getListTextFieldBlok4().get(i).isEnabled()) {
                    allFilled = false;
                }
            }
            if (allFilled == true) {
                if (Integer.parseInt(field.getText()) >= 1 && Integer.parseInt(field.getText()) <= 2) {
                    if (panelBlok4.getListTextFieldBlok4().get(index - 1).getText().equals("2")) {
                        if (panelBlok4.getListTextFieldBlok4().get(2).getText().equals("1")) {
                            if (panelBlok4.getListTextFieldBlok4().get(index).getText().equals("2")) {
                                return true;
                            } else {
                                errorMessage("JK untuk suami/istri harus berbeda dengan JK KRT!");
                                return false;
                            }
                        } else {
                            if (panelBlok4.getListTextFieldBlok4().get(index).getText().equals("1")) {
                                return true;
                            } else {
                                errorMessage("JK untuk suami/istri harus berbeda dengan JK KRT!");
                                return false;
                            }
                        }
                    } else {
                        return true;
                    }
                } else {
                    errorMessage("Nilai field antara 1 atau 2!");
                    return false;
                }
            } else {
                errorMessage("Isi Field sebelumnya terlebih dahulu!");
                return false;
            }

        }
    }

    private class ValidasiB4C5 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = panelBlok4.getListTextFieldBlok4().indexOf(field);
            boolean allFilled = true;
            for (int i = 0; i < index; i++) {
                if (panelBlok4.getListTextFieldBlok4().get(i).getText().equals("") && panelBlok4.getListTextFieldBlok4().get(i).isEnabled()) {
                    allFilled = false;
                }
            }
            int rowNumber = (index + 7) % 10;              

            if (allFilled == true) {
                if (Integer.parseInt(field.getText()) >= 0 && Integer.parseInt(field.getText()) <= 98) {
                    if (Integer.parseInt(field.getText()) < 10) {
                        if(scanUmur(rowNumber, panelBlok4.getListTextFieldBlok4().get(index - 2).getText(),field.getInt())==false){
                            return false;
                        }
                        if ((Integer.parseInt(PanelBlok123.getInstance().getTextFieldB2R1().getText()) == (rowNumber + 1))) {
                            int jmlhArtLb10 = 0;
                            for (int i = 0; i < (rowNumber + 1); i++) {
                                int listIndex = 3 + i + (i * 10);
                                if (Integer.parseInt(panelBlok4.getListTextFieldBlok4().get(listIndex).getText()) >= 10) {
                                    jmlhArtLb10 = jmlhArtLb10 + 1;
                                }
                            }

                            if (jmlhArtLb10 != Integer.parseInt(PanelBlok123.getInstance().getTextFieldB2R2().getText())) {
                                errorMessage("Jumlah ART di atas 10 tahun tidak sesuai!");

                                return false;
                            } else {
                                panelBlok4.getListTextFieldBlok4().get(index + 1).setText("");
                                panelBlok4.getListTextFieldBlok4().get(index + 1).setEnabled(false);
                                if (Integer.parseInt(field.getText()) >= 5) {
                                    panelBlok4.getListTextFieldBlok4().get(index + 7).setEnabled(true);
                                }
                                dataRuta[rowNumber][0]=panelBlok4.getListTextFieldBlok4().get(index - 2).getText();
                                dataRuta[rowNumber][1]=field.getText();
                                return true;
                            }
                        } else if (panelBlok4.getListTextFieldBlok4().get(index - 2).getText().equals("1")
                                || panelBlok4.getListTextFieldBlok4().get(index - 2).getText().equals("2")
                                || panelBlok4.getListTextFieldBlok4().get(index - 2).getText().equals("4")
                                || panelBlok4.getListTextFieldBlok4().get(index - 2).getText().equals("6")) {
                            errorMessage("Umur harus lebih dari 10 tahun");

                            return false;
                        } else {
                            panelBlok4.getListTextFieldBlok4().get(index + 1).setText("");
                            panelBlok4.getListTextFieldBlok4().get(index + 1).setEnabled(false);
                            if (Integer.parseInt(field.getText()) >= 5) {
                                panelBlok4.getListTextFieldBlok4().get(index + 7).setEnabled(true);
                            }
                            dataRuta[rowNumber][0]=panelBlok4.getListTextFieldBlok4().get(index - 2).getText();
                            dataRuta[rowNumber][1]=field.getText();
                            return true;
                        }
                    } else {
                        if(scanUmur(rowNumber, panelBlok4.getListTextFieldBlok4().get(index - 2).getText(),field.getInt())==false){
                            return false;
                        }
                        if ((Integer.parseInt(PanelBlok123.getInstance().getTextFieldB2R1().getText()) == (rowNumber + 1))) {
                            int jmlhArtLb10 = 0;
                            for (int i = 0; i < (rowNumber + 1); i++) {
                                int listIndex = 3 + i + (i * 10);
                                if (Integer.parseInt(panelBlok4.getListTextFieldBlok4().get(listIndex).getText()) >= 10) {
                                    jmlhArtLb10 = jmlhArtLb10 + 1;
                                }

                            }
                            if (jmlhArtLb10 != Integer.parseInt(PanelBlok123.getInstance().getTextFieldB2R2().getText())) {
                                errorMessage("Jumlah ART di atas 10 tahun tidak sesuai!");

                                return false;
                            } else {
                                panelBlok4.getListTextFieldBlok4().get(index + 7).setEnabled(true);
                                dataRuta[rowNumber][0]=panelBlok4.getListTextFieldBlok4().get(index - 2).getText();
                                dataRuta[rowNumber][1]=field.getText();
                                return true;
                            }
                        } else {
                            panelBlok4.getListTextFieldBlok4().get(index + 1).setEnabled(true);
                            panelBlok4.getListTextFieldBlok4().get(index + 7).setEnabled(true);
                            dataRuta[rowNumber][0]=panelBlok4.getListTextFieldBlok4().get(index - 2).getText();
                            dataRuta[rowNumber][1]=field.getText();
                            return true;
                        }
                    }
                } else {
                    errorMessage("Umur antara 0-98!");
                    return false;
                }
            } else {
                errorMessage("Isi Field sebelumnya terlebih dahulu!");
                return false;
            }
        }
        private boolean scanUmur(int currentRow, String status, int umur){
            if(status.equals("5")){
                for(int i = 0; i< currentRow ; i++){
                    if(dataRuta[i][0].equals("1")||dataRuta[i][0].equals("2")||dataRuta[i][0].equals("3")||dataRuta[i][0].equals("4")||dataRuta[i][0].equals("6")){
                        if(umur>Integer.parseInt(dataRuta[i][1])){
                            errorMessage("Umur cucu tidak sesuai");
                            return false;
                        }
                    } 
                }
            }
            else if(status.equals("3")){
                for(int i = 0; i< currentRow ; i++){
                    if(dataRuta[i][0].equals("1")||dataRuta[i][0].equals("2")||dataRuta[i][0].equals("6")){
                        if(umur>Integer.parseInt(dataRuta[i][1])){
                            errorMessage("Umur anak tidak sesuai");
                            return false;
                        }
                    } 
                }
            }
            return true;
        }
    }

    private class ValidasiB4C6 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = panelBlok4.getListTextFieldBlok4().indexOf(field);
            boolean allFilled = true;
            for (int i = 0; i < index; i++) {
                if (panelBlok4.getListTextFieldBlok4().get(i).getText().equals("") && panelBlok4.getListTextFieldBlok4().get(i).isEnabled()) {
                    allFilled = false;
                }
            }
            if (allFilled == true) {
                if (Integer.parseInt(field.getText()) >= 1 && Integer.parseInt(field.getText()) <= 4) {
                    if (panelBlok4.getListTextFieldBlok4().get(index - 3).getText().equals("2")) {
                        if (panelBlok4.getListTextFieldBlok4().get(index).getText().equals("2")) {
                            return true;
                        } else {
                            errorMessage("Nilai status harus kawin!");
                            return false;
                        }
                    } else if (panelBlok4.getListTextFieldBlok4().get(index - 3).getText().equals("4") || panelBlok4.getListTextFieldBlok4().get(index - 3).getText().equals("6")) {
                        if (!panelBlok4.getListTextFieldBlok4().get(index).getText().equals("1")) {
                            return true;
                        } else {
                            errorMessage("Nilai status harus kawin, cerai hidup atau cerai mati!");
                            return false;
                        }
                    } else {
                        return true;
                    }
                } else {
                    errorMessage("Nilai field antara 1-4!");
                    return false;
                }
            } else {
                errorMessage("Isi Field sebelumnya terlebih dahulu!");
                return false;
            }
        }
    }

    private class ValidasiB4C7 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = panelBlok4.getListTextFieldBlok4().indexOf(field);
            boolean allFilled = true;
            for (int i = 0; i < index; i++) {
                if (panelBlok4.getListTextFieldBlok4().get(i).getText().equals("") && panelBlok4.getListTextFieldBlok4().get(i).isEnabled()) {
                    allFilled = false;
                }
            }
            if (allFilled == true) {
                if (Integer.parseInt(field.getText()) >= 1 && Integer.parseInt(field.getText()) <= 4) {

                    if (panelBlok4.getListTextFieldBlok4().get(index).getText().equals("1")) {
                        if (!panelBlok4.getListTextFieldBlok4().get(index + 1).isEnabled()) {
                            panelBlok4.getListTextFieldBlok4().get(index + 1).setEnabled(true);
                        }
                        if (!panelBlok4.getListTextFieldBlok4().get(index + 2).isEnabled()) {
                            panelBlok4.getListTextFieldBlok4().get(index + 2).setEnabled(true);
                        }

                        panelBlok4.getListTextFieldBlok4().get(index + 1).setRequired(true);
                        panelBlok4.getListTextFieldBlok4().get(index + 2).setRequired(true);
                        return true;
                    } else {
                        panelBlok4.getListTextFieldBlok4().get(index + 1).setText("");
                        panelBlok4.getListTextFieldBlok4().get(index + 1).setEnabled(false);
                        panelBlok4.getListTextFieldBlok4().get(index + 2).setText("");
                        panelBlok4.getListTextFieldBlok4().get(index + 2).setEnabled(false);
                        return true;
                    }
                } else {
                    errorMessage("Nilai field antara 1-4!");
                    return false;
                }
            } else {
                errorMessage("Isi Field sebelumnya terlebih dahulu!");
                return false;
            }
        }
    }

    private class ValidasiB4C8 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = panelBlok4.getListTextFieldBlok4().indexOf(field);
            boolean allFilled = true;
            for (int i = 0; i < index; i++) {
                if (panelBlok4.getListTextFieldBlok4().get(i).getText().equals("") && panelBlok4.getListTextFieldBlok4().get(i).isEnabled()) {
                    allFilled = false;
                }
            }
            if (allFilled == true) {
                if (Integer.parseInt(field.getText()) >= 1 && Integer.parseInt(field.getText()) <= 14) {

                    if (panelBlok4.getListTextFieldBlok4().get(index - 1).getText().equals("1")) {
                        if (panelBlok4.getListTextFieldBlok4().get(index).getText().equals("")) {
                            errorMessage("Nilai field harus diisi!");
                            return false;
                        } else {
                            return true;
                        }
                    } else {
                        return true;
                    }
                } else {
                    errorMessage("Nilai field antara 1-14!");
                    return false;
                }
            } else {
                errorMessage("Isi Field sebelumnya terlebih dahulu!");
                return false;
            }
        }
    }

    private class ValidasiB4C9 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = panelBlok4.getListTextFieldBlok4().indexOf(field);
            boolean allFilled = true;
            for (int i = 0; i < index; i++) {
                if (panelBlok4.getListTextFieldBlok4().get(i).getText().equals("") && panelBlok4.getListTextFieldBlok4().get(i).isEnabled()) {
                    allFilled = false;
                }
            }
            if (allFilled == true) {
                if (Integer.parseInt(field.getText()) >= 1 && Integer.parseInt(field.getText()) <= 6) {

                    if (panelBlok4.getListTextFieldBlok4().get(index - 2).getText().equals("1")) {
                        if (panelBlok4.getListTextFieldBlok4().get(index).getText().equals("")) {
                            errorMessage("Nilai field harus diisi!");
                            return false;
                        } else {
                            return true;
                        }
                    } else {
                        return true;
                    }
                } else {
                    errorMessage("Nilai field antara 1-5!");
                    return false;
                }
            } else {
                errorMessage("Isi Field sebelumnya terlebih dahulu!");
                return false;
            }
        }
    }

    private class ValidasiB4C10 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = panelBlok4.getListTextFieldBlok4().indexOf(field);
            boolean allFilled = true;
            for (int i = 0; i < index; i++) {
                if (panelBlok4.getListTextFieldBlok4().get(i).getText().equals("") && panelBlok4.getListTextFieldBlok4().get(i).isEnabled()) {
                    allFilled = false;
                }
            }
            int rowNumber = (index + 2) % 10;
            if (allFilled == true) {
                if (Integer.parseInt(field.getText()) >= 1 && Integer.parseInt(field.getText()) <= 3) {
                    if (!panelBlok4.getListTextFieldBlok4().get(index).getText().equals("1")) {
                        if (!panelBlok4.getListTextFieldBlok4().get(index + 1).isEnabled()) {
                            panelBlok4.getListTextFieldBlok4().get(index + 1).setEnabled(true);
                            panelBlok4.getListTextFieldBlok4().get(index + 1).setRequired(true);
                        }
                        return true;
                    } else {

                        if (Integer.parseInt(panelBlok4.getListTextFieldBlok4().get(index - 5).getText()) < 5) {
                            panelBlok4.getListTextFieldBlok4().get(index + 1).setText("");
                            panelBlok4.getListTextFieldBlok4().get(index + 1).setEnabled(false);
                            panelBlok4.getListTextFieldBlok4().get(index + 2).setText("");
                            panelBlok4.getListTextFieldBlok4().get(index + 2).setEnabled(false);
                            if ((Integer.parseInt(PanelBlok123.getInstance().getTextFieldB2R1().getText()) == (rowNumber + 1))) {
                                CardKues.getCard().next();
                            }
                        } else {
                            panelBlok4.getListTextFieldBlok4().get(index + 1).setText("");
                            panelBlok4.getListTextFieldBlok4().get(index + 1).setEnabled(false);
                            panelBlok4.getListTextFieldBlok4().get(index + 2).setEnabled(true);
                            panelBlok4.getListTextFieldBlok4().get(index + 2).setRequired(true);
                        }

                        return true;
                    }
                } else {
                    errorMessage("Nilai field antara 1-3!");
                    return false;
                }
            } else {
                errorMessage("Isi Field sebelumnya terlebih dahulu!");
                return false;
            }
        }
    }

    private class ValidasiB4C11 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = panelBlok4.getListTextFieldBlok4().indexOf(field);
            boolean allFilled = true;
            for (int i = 0; i < index; i++) {
                if (panelBlok4.getListTextFieldBlok4().get(i).getText().equals("") && panelBlok4.getListTextFieldBlok4().get(i).isEnabled()) {
                    allFilled = false;
                }
            }
            int rowNumber = (index + 1) % 10;
            if (allFilled == true) {
                if (Integer.parseInt(field.getText()) >= 1 && Integer.parseInt(field.getText()) <= 6) {

                    if ((Integer.parseInt(PanelBlok123.getInstance().getTextFieldB2R1().getText()) == (rowNumber + 1))
                            && (Integer.parseInt(panelBlok4.getListTextFieldBlok4().get(index - 6).getText()) < 5)) {
                        CardKues.getCard().next();
                    }
                    return true;
                } else {
                    errorMessage("Nilai field antara 1-6!");
                    return false;
                }
            } else {
                errorMessage("Isi Field sebelumnya terlebih dahulu!");
                return false;
            }
        }
    }

    private class ValidasiB4C12 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = panelBlok4.getListTextFieldBlok4().indexOf(field);
            boolean allFilled = true;
            for (int i = 0; i < index; i++) {
                if (panelBlok4.getListTextFieldBlok4().get(i).getText().equals("") && panelBlok4.getListTextFieldBlok4().get(i).isEnabled()) {
                    allFilled = false;
                }
            }
            int rowNumber = (index) % 10;
            if (allFilled == true) {

                if (Integer.parseInt(field.getText()) >= 1 && Integer.parseInt(field.getText()) <= 2) {
                    if(field.getText().equals("2") && panelBlok4.getListTextFieldBlok4().get(index - 1).getInt()>3){
                        errorMessage("Periksa kembali realibilitas bisa baca tulis dengan tingkat pendidikan");
                        return false;
                    }
                    else{
                        if ((Integer.parseInt(PanelBlok123.getInstance().getTextFieldB2R1().getText()) == (rowNumber + 1))) {
                            CardKues.getCard().next();
                        }
                        return true;
                    }
                } else {
                    errorMessage("Nilai field antara 1 atau 2!");
                    return false;
                }
            } else {
                errorMessage("Isi Field sebelumnya terlebih dahulu!");
                return false;
            }
        }
    }
}
